package vip.mtdp.asset.core.manager;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.mtdp.asset.core.constants.PostingStateEnum;
import vip.mtdp.asset.core.dal.dao.JournalItemMapper;
import vip.mtdp.asset.core.dal.dao.JournalMapper;
import vip.mtdp.asset.core.dal.domain.Journal;
import vip.mtdp.asset.core.dal.domain.JournalItem;
import vip.mtdp.asset.core.dto.JournalDTO;
import vip.mtdp.common.exception.AppRTException;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Description
 * @Author Evans.wang
 * @since 2023/10/29 12:16
 **/
@Slf4j
@Service
public class JournalManager {

    @Autowired
    private JournalMapper journalMapper;

    @Autowired
    private JournalItemMapper journalItemMapper;

    /**
     * 创建journal
     * @param journalDTO
     * @return
     */
    @Transactional
    public JournalDTO create(JournalDTO journalDTO){
        Journal journal = new Journal();
        BeanUtil.copyProperties(journalDTO, journal);
        if (journalDTO.getPostingState() == null){
            journal.setPostingState(PostingStateEnum.INIT.getCode());
        }
        //默认当前时间为，记账日期
        if (journalDTO.getAccountingDate() == null){
            journal.setAccountingDate(new Date());
        }
        int cnt = journalMapper.insert(journal);

        List<JournalDTO.JournalItemDTO> journalItemDTOList = journalDTO.getJournalItemDTOList();
        if (journalItemDTOList != null && !journalItemDTOList.isEmpty()){
            List<JournalItem> journalItemList = new ArrayList<>();
            journalItemDTOList.forEach(e -> {
                JournalItem journalItem = new JournalItem();
                BeanUtil.copyProperties(e, journalItem);
                journalItem.setJournalId(journal.getId());
                Date d = new Date();
                journalItem.setCreateTime(d);
                journalItem.setUpdateTime(d);
                journalItemList.add(journalItem);
            });
            int cntItem = journalItemMapper.batchInsert(journalItemList);
            if (cntItem != journalItemDTOList.size()){
                log.warn("create journal item fail. txnId = {}", journalDTO.getTxnId());
                throw new AppRTException("create journal fail");
            }
        }
        if (cnt != 1){
            throw new AppRTException("create journal fail");
        }
        journalDTO.setId(journal.getId());
        journalDTO.setPostingState(journal.getPostingState());
        journalDTO.setAccountingDate(journal.getAccountingDate());
        return journalDTO;
    }

    /**
     * 更新记账状态
     * @param journalId
     * @param postingStateEnum
     * @return
     */
    public boolean updatePostingState(Long journalId, PostingStateEnum postingStateEnum){
        Journal journal = new Journal();
        journal.setId(journalId);
        journal.setPostingState(postingStateEnum.getCode());
        return journalMapper.updateById(journal) == 1;
    }

    /**
     * 根据txnId查询分录
     * @param txnId
     * @return
     */
    public List<JournalDTO> queryByTxnId(Long txnId){
        QueryWrapper<Journal> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Journal::getTxnId, txnId);
        List<JournalDTO> journalDTOList = new ArrayList<>();
        List<Journal> journalList = journalMapper.selectList(queryWrapper);
        if (journalList != null && !journalList.isEmpty()){
            for (Journal journal : journalList) {
                JournalDTO journalDTO = new JournalDTO();
                BeanUtil.copyProperties(journal, journalDTO);

                journalDTOList.add(journalDTO);

                QueryWrapper<JournalItem> mQueryWrapper = new QueryWrapper<>();
                mQueryWrapper.lambda().eq(JournalItem::getJournalId, journal.getId());
                List<JournalItem> journalItemList = journalItemMapper.selectList(mQueryWrapper);

                List<JournalDTO.JournalItemDTO> journalItemDTOList = new ArrayList<>();

                journalItemList.forEach(e -> {
                    JournalDTO.JournalItemDTO journalItemDTO = new JournalDTO.JournalItemDTO();
                    BeanUtil.copyProperties(e, journalItemDTO);
                    journalItemDTOList.add(journalItemDTO);
                });

                journalDTO.setJournalItemDTOList(journalItemDTOList);
            }
        }
        return journalDTOList;
    }

}
